<?php
class BuyAccountModel extends Db {
	private $_buy_account = 'd_buy_account';
	
	public function addAccount($params) {
		return $this->add ( $this->_buy_account, $params );
	}
	
	public function getAccountByUid($start, $num, $uid) {
		return $this->getAccountByWhere ( $start, $num, array ('uid' => $uid ) );
	}
	
	public function getAccountByStatus($start, $num, $status) {
		if ($status === "all") {
			return $this->getAccountByWhere ( $start, $num, null );
		}
		
		return $this->getAccountByWhere ( $start, $num, array ('status' => $status ) );
	}
	
	public function countAcountByUid($uid) {
		$sql = "SELECT id FROM $this->_buy_account WHERE uid=$uid";
		return $this->rowCount ( $sql );
	}
	
	public function countAcountByStatus($status) {
		if ($status === "all") {
			return $this->countAccount ();
		}
		
		$sql = "SELECT id FROM $this->_buy_account WHERE status=$status";
		return $this->rowCount ( $sql );
	}
	
	public function countAccount() {
		$sql = "SELECT id FROM $this->_buy_account";
		return $this->rowCount ( $sql );
	}
	
	public function getAccountByWhere($start, $num, $where) {
		return $this->getPage ( $start, $num, $this->_buy_account, null, $where, 'id DESC' );
	}
	
	public function removeAccountBySNO($orderno) {
		return $this->delete ( $this->_buy_account, array ('orderno' => $orderno ) );
	}
	
	public function setAccountStatusBySNO($status, $orderno) {
		return $this->update ( $this->_buy_account, array ('status' => $status ), array ('orderno' => $orderno ) );
	}
	
    public function setAccountMoneyBySNO($money, $orderno) {
		return $this->update ( $this->_buy_account, array ('money' => $money ), array ('orderno' => $orderno ) );
	}
	
	/**
	 * 
	 * @return BuyAccountModel
	 */
	public static function instance() {
		return parent::_instance ( __CLASS__ );
	}
}